package net.hyperz.showtime.ui;

import java.text.DecimalFormat;
import java.util.Date;

import net.hyperz.showtime.R;
import net.hyperz.showtime.utils.Data.Network;
import net.hyperz.showtime.utils.Data.Show;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public abstract class Utility {
	
	public static void CreateGridNetworkColumn(Context context, Network network, TableRow row) {
		TableRow.LayoutParams trlp = TableRowLayout(0, (int)(context.getResources().getDisplayMetrics().densityDpi * .25));
		// make a new vertical LinearLayout to hold the views
		LinearLayout colChild = VerticalLinearLayout(context);
		//create layout params for this view
		LinearLayout.LayoutParams llp = TableColLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		//first column is the network or channel column
		LayoutInflater li = ((Activity) context).getLayoutInflater();
		View networkItem = li.inflate(R.layout.grid_item_network, null);
		
		String channel = (Math.floor(network.Channel) == network.Channel) 
				? Integer.toString((int)network.Channel) 
				: Double.toString(network.Channel);
		
		((TextView)networkItem.findViewById(R.id.grid_item_network_channel_num)).setText(channel);
		((TextView)networkItem.findViewById(R.id.grid_item_network_channel_id)).setText(network.CallSign);
		//((ImageView)networkItem.findViewById(R.id.grid_item_network_icon)).setImageBitmap(networks.get(x).Logo);
		
		networkItem.setBackgroundResource(R.drawable.data_border_news);
		colChild.addView(networkItem, llp);
		
		row.addView(colChild, trlp);
	}
	
	public static float CreateGridTimeSlot(final Context context, final Show show, TableRow row) {
		TableRow.LayoutParams trlp = TableRowLayout(0, (int)(context.getResources().getDisplayMetrics().densityDpi * .25));
		// make a new vertical LinearLayout to hold the views
		LinearLayout colChild = VerticalLinearLayout(context);
		//create layout params for this view
		LinearLayout.LayoutParams llp = TableColLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		
		//each column is in actuality a 30 min block
		// therefore our formula for determining weight is Show.Length / 30
		Long airTime = show.AirTime; 
		trlp.weight =  Float.valueOf(new DecimalFormat("#.##").format(Float.parseFloat(airTime.toString()) / 30));
		
		//generate a text view to sit within the layout, it's text will be the time slot
		TextView showSlot = new TextView(context);
		showSlot.setText(show.Name);
		
		colChild.addView(showSlot, llp);
		colChild.setBackgroundResource(R.drawable.data_border_fav);
		
		colChild.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(context, new Date(show.StartTime) + " - " + show.Name, Toast.LENGTH_LONG).show();
			}
		});
		
		row.addView(colChild, trlp);
		
		return trlp.weight;
	}
	
	private static LinearLayout VerticalLinearLayout(Context context) {
		// make a new vertical LinearLayout to hold the views
		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(LinearLayout.VERTICAL);
		
		return ll;
	}
	
	public static TableRow.LayoutParams TableRowLayout(float weight) {
		return TableRowLayout(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, weight);
	}
	
	public static TableRow.LayoutParams TableRowLayout(int w, int h) {
		return TableRowLayout(w, h, 1f);
	}
	
	public static TableRow.LayoutParams TableRowLayout(int w, int h, float weight) {
		return new TableRow.LayoutParams(w, h, weight);
	}
	
	public static LinearLayout.LayoutParams TableColLayout(float weight) {
		return TableColLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
	}
	
	public static LinearLayout.LayoutParams TableColLayout(int w, int h) {
		return TableColLayout(w, h, 1f);
	}
	
	public static LinearLayout.LayoutParams TableColLayout(int w, int h, float weight) {
		//create layout params for this view
		LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(w, h, weight);
		llp.gravity = Gravity.TOP;
		return llp;
	}
}
